package com.lx.test;

public class blocksearch {
    public static void main(String[] args) {
        int arr[] = {1,2,21,
                23,27,25,29,
                30,32,41,50};

        block b1 = new block(21,0,2);
        block b2 = new block(29,3,6);
        block b3 = new block(50,7,10);
        block blockarr[] = {b1,b2,b3};
        int num = 20;
        int i = Search(blockarr,arr,num);
        System.out.println(i);
    }

    private static int Search(block[] blockarr, int[] arr, int num) {
        int index = searchindex(blockarr,num);
        if(index == -1)
        {
            return -1;
        }
        for(int i=blockarr[index].getStartindex();i<blockarr[index].getEndindex();i++)
        {
            if(arr[i]==num)
            {
                return i;
            }
        }
        return -1;
    }

    private static int searchindex(block[] blockarr, int num) {
        for(int i=0;i<blockarr.length;i++)
        {
            if(num<=blockarr[i].getMax())
            {
                return i;
            }
        }
        return -1;
    }
}

class block{
    private int max;
    private  int startindex;
    private int endindex;

    public block() {
    }

    public block(int max, int startindex, int endindex) {
        this.max = max;
        this.startindex = startindex;
        this.endindex = endindex;
    }

    /**
     * 获取
     * @return max
     */
    public int getMax() {
        return max;
    }

    /**
     * 设置
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 获取
     * @return startindex
     */
    public int getStartindex() {
        return startindex;
    }

    /**
     * 设置
     * @param startindex
     */
    public void setStartindex(int startindex) {
        this.startindex = startindex;
    }

    /**
     * 获取
     * @return endindex
     */
    public int getEndindex() {
        return endindex;
    }

    /**
     * 设置
     * @param endindex
     */
    public void setEndindex(int endindex) {
        this.endindex = endindex;
    }

    public String toString() {
        return "block{max = " + max + ", startindex = " + startindex + ", endindex = " + endindex + "}";
    }
}